package com.shanghaionstar.utils;

import com.onstar.cn.common.log.LogManager;
import com.onstar.cn.framework.mvp.base.core.BaseActivity;
import com.onstar.cn.more.response.DataPackageEntity;
import com.onstar.cn.payme.response.GetPackageListResponse;
import com.shanghaionstar.carmanage.iview.ICarFlowDataViewImpl;
import com.shanghaionstar.carmanage.presenter.CarFlowDataPresenter;
import com.shanghaionstar.servicepackage.presenter.OnstarPkgPresenter;
import com.shanghaionstar.servicepackage.view.OnstarPkgView;

/**
 * Created by Yonglang Hong on 2016/8/26.
 */
public class UserCenterCacheHelper {

    public static final Object asyncObj = new Object();

    private static UserCenterCacheHelper mInstance;

    /**
     * 安吉星套餐
     */
    private GetPackageListResponse packageEntity_onstar;

    /**
     * 4G套餐
     */
    private DataPackageEntity packageEntity_4g;

    public static UserCenterCacheHelper getInstance() {
        synchronized (asyncObj) {
            if (mInstance == null) {
                mInstance = new UserCenterCacheHelper();
            }
            return mInstance;
        }
    }

    public GetPackageListResponse getPackageEntity_onstar() {
        return packageEntity_onstar;
    }

    public void setPackageEntity_onstar(GetPackageListResponse packageEntity_onstar) {
        this.packageEntity_onstar = packageEntity_onstar;
    }

    public DataPackageEntity getPackageEntity_4g() {
        return packageEntity_4g;
    }

    public void setPackageEntity_4g(DataPackageEntity packageEntity_4g) {
        this.packageEntity_4g = packageEntity_4g;
    }

    private CarFlowDataPresenter presenter_4g;

    private OnstarPkgPresenter presenter_onstar;

    public CarFlowDataPresenter getPresenter_4g(BaseActivity activity) {
        if (presenter_4g == null) {
            presenter_4g = new CarFlowDataPresenter(activity.getScheduler(), activity);
            presenter_4g.setFlowDataView(new ICarFlowDataViewImpl() {
                @Override
                public void showFlowData(DataPackageEntity flowData) {
                    isfinish_4g = true;
                    LogManager.getInstance().d("UserCenterCacheHelper is caching 4g package:"+flowData.getCurrentRemainUsage());
                    doIfComplete();
                    setPackageEntity_4g(flowData);
                }

                @Override
                public void showErrorGetmsg(boolean isShow, Throwable throwable) {
                    isfinish_4g = true;
                    doIfComplete();
                }
            });
        }
        return presenter_4g;
    }

    public OnstarPkgPresenter getPresenter_onstar(BaseActivity activity) {
        if (presenter_onstar == null) {
            presenter_onstar = new OnstarPkgPresenter(activity.getScheduler(), activity);
            presenter_onstar.setPackageView(new OnstarPkgView() {
                @Override
                public void showNoDatas() {
                    isfinish_onstar = true;
                    doIfComplete();
                }

                @Override
                public void showError(String err) {
                    isfinish_onstar = true;
                    doIfComplete();
                }

                @Override
                public void showPackageList(GetPackageListResponse listResponse) {
                    isfinish_onstar = true;
                    LogManager.getInstance().d("UserCenterCacheHelper is caching onstar package:"+listResponse.getTotalRemainingDay());
                    setPackageEntity_onstar(listResponse);
                    doIfComplete();
                }
            });
        }
        return presenter_onstar;
    }

    private void doIfComplete() {
        synchronized (asyncObj) {
            if (isfinish_4g && isfinish_onstar && callBack != null) {
                callBack.onLoadComplete();
            }
        }
    }

    boolean isfinish_4g = true, isfinish_onstar = true;

    /**
     * 更新数据
     * @param activity
     */
    public void updateAsync(BaseActivity activity) {
        isfinish_4g = false;
        getPresenter_4g(activity).getFlowData();
        isfinish_onstar = false;
        getPresenter_onstar(activity).getOnstarPackages();
    }

    loadCallBack callBack;

    public void setCacheCallBack(loadCallBack callBack) {
        this.callBack = callBack;
    }

    public interface loadCallBack {
        void onLoadComplete();
    }

    public boolean hasCached4g(){
        return packageEntity_4g != null;
    }

    public boolean hasCachedOnstar(){
        return packageEntity_onstar != null;
    }
}
